Spatial Data in R

Customizing maps in leaflet

Today’s agenda

Customizing maps

  • Search feature
  • Adding control features
  • Cluster options (too much data!)
  • Adding shape files to our map!

leaflet: search engine

  • A search feature can be interesting for interaction
  • And very useful to confirm geolocation data
# Loading an extra package (which cointains the function we want)
install.packages('leaflet.extras')
library(leaflet.extras)

# Adding the searchOSM feature
cps_schools |> 
  leaflet() |> 
  addProviderTiles("CartoDB") |> 
  addCircleMarkers(lng = ~school_long,
                   lat = ~school_lat,
                   radius = 5,
                   color = "red",
                   label = ~school_nm) |> 
  addSearchOSM()

leaflet: group creation

  • A search feature can be interesting for interaction
  • And very useful to confirm geolocation data
## Changing the mapping method
# And adding the `color` and `group` parameters within the calls
leaflet() |> 
  addProviderTiles("CartoDB") |> 
  addCircleMarkers(lng = cps_schools_blue$school_long,
                   lat = cps_schools_blue$school_lat,
                   radius = 5,
                   color = 'blue',
                   group = 'selected',
                   label = cps_schools_blue$school_nm) |> 
    addCircleMarkers(lng = cps_schools_red$school_long,
                   lat = cps_schools_red$school_lat,
                   radius = 5,
                   color = 'red',
                   group = 'control',
                   label = cps_schools_red$school_nm) |> 
  addSearchOSM()

leaflet: clustering

  • When we have too much data (too much points), the map can get crowded, and a good option to solve this is to cluster the points!
## Clustering the points in the graph
leaflet() |> 
  addProviderTiles("CartoDB") |> 
  addCircleMarkers(lng = cps_schools_blue$school_long,
                   lat = cps_schools_blue$school_lat,
                   radius = 5,
                   color = "blue",
                   group = 'selected',
                   label = cps_schools_blue$school_nm,
                   clusterOptions = markerClusterOptions()) |> 
    addCircleMarkers(lng = cps_schools_red$school_long,
                   lat = cps_schools_red$school_lat,
                   radius = 5,
                   color = "red",
                   group = 'control',
                   label = cps_schools_red$school_nm,
                   clusterOptions = markerClusterOptions()) |> 
  addSearchOSM()

leaflet: adding polygons

  • We can also add polygons
  • Normally, .shp type of files
  • It’s a different data structure in R
# Reading a shape file with `st_read` from the sf package
library(sf)
chicago_districs <- st_read('chicago-boundaries/chciago_shape.shp')

## Clustering the points in the graph
leaflet() |> 
  addProviderTiles("CartoDB") |> 
  addCircleMarkers(lng = cps_schools_blue$school_long,
                   lat = cps_schools_blue$school_lat,
                   radius = 5,
                   color = "blue",
                   group = 'selected',
                   label = cps_schools_blue$school_nm,
                   clusterOptions = markerClusterOptions()) |> 
    addCircleMarkers(lng = cps_schools_red$school_long,
                   lat = cps_schools_red$school_lat,
                   radius = 5,
                   color = "red",
                   group = 'control',
                   label = cps_schools_red$school_nm,
                   clusterOptions = markerClusterOptions()) |> 
  addSearchOSM() |> 
  ## Adding the shape file as the final layer
  addPolygons(data = chicago_districs,
              weight = 2,
              opacity = 0.5,
              color = 'orange',
              dashArray = '3',
              fillOpacity = 0.2)

Lecture recap

  • We’ve learned how to:
    • Add a search feature to our map
    • Add user control features
    • Numerical clusterize our data when needed
    • Add shape files to our map!

Stay tuned!